package com.email.handler;

import com.email.service.MailService;
import com.leyou.api.ActivityService;
import com.leyou.api.ReceiveService;
import com.leyou.api.TurnService;
import com.leyou.domain.db1entity.Email;
import com.leyou.domain.vo.ReceiveVO;
import com.leyou.domain.vo.TurnRecordVO;
import com.leyou.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


/**
 * @Author lixin
 * @Date 2018/11/01 17:36
 * @Version 1.0
 * @Description 消息消费
 **/
@Component
public class EmailHandler {


    @Autowired
    TemplateEngine templateEngine;

    @Autowired
    RedisTemplate redisTemplate;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    MailService mailService;



    @RabbitHandler
    @RabbitListener(queues = "email")
    public void process(Email email) {
        email.setId(Utils.getUUID(32));
        email.setTrycount(1);
        switch (email.getType()) {
            case "1":
                String code = Utils.getUUID(6);
                Context context = new Context();
                context.setVariable("id", code);
                String emailContent = templateEngine.process("emailTemplate", context);
                try {
                    mailService.sendHtmlMail(email.getTo(), "注册邮件", emailContent);
                } catch (Exception e) {
                    email.setStatus("0");
                    e.printStackTrace();
                    return;
                }
                email.setStatus("1");
                redisTemplate.opsForValue().set(email.getTo(), code, 30, TimeUnit.MINUTES);
                break;
            case "2":
                Context context2 = new Context();
                String emailContent2 = templateEngine.process("reg_success", context2);
                try {
                    mailService.sendHtmlMail(email.getTo(), "注册邮件", emailContent2);
                } catch (Exception e) {
                    email.setStatus("0");
                    e.printStackTrace();
                    return;
                }
                email.setStatus("1");
                break;
            case "3":
                String code2 = Utils.getUUID(6);
                Context context3 = new Context();
                context3.setVariable("id", code2);
                String emailContent3 = templateEngine.process("emailTemplate", context3);
                try {
                    mailService.sendHtmlMail(email.getTo(), "找回密码", emailContent3);
                } catch (Exception e) {
                    email.setStatus("0");
                    e.printStackTrace();
                    return;
                }
                email.setStatus("1");
                redisTemplate.opsForValue().set(email.getTo(), code2, 30, TimeUnit.MINUTES);
                break;
            default:
                break;
        }
    }
}
